package com.jw.qme.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jw.qme.domain.dto.ChatConversationDTO;
import com.jw.qme.domain.dto.MessageDTO;
import com.jw.qme.domain.po.ChatConversation;
import com.jw.qme.domain.vo.ChatConversationVO;
import com.jw.qme.domain.vo.MessageVO;
import com.jw.qme.mapper.ChatConversationMapper;
import com.jw.qme.service.ChatConversationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ChatConversationServiceImpl extends ServiceImpl<ChatConversationMapper, ChatConversation> implements ChatConversationService {

    @Autowired
    private ChatConversationMapper chatConversationMapper;

    @Autowired
    private MessageServiceImpl messageService;

    @Override
    public ChatConversationVO create(ChatConversationDTO chatConversationDTO) {
        // 先去数据库中进行查询 如果有的话直接拿出来
        ChatConversation chatConversation = new ChatConversation(chatConversationDTO.getUserId(),chatConversationDTO.getFriendId(),chatConversationDTO.getOrderNo());
        int insert = chatConversationMapper.insert(chatConversation);
        // 构建返回对象
        ChatConversationVO chatConversationVO = new ChatConversationVO();
        BeanUtils.copyProperties(chatConversation,chatConversationVO);
        chatConversationVO.setCreateTime(LocalDateTime.now());
        chatConversationVO.setUnreadCount(0);
        chatConversationVO.setIsShow(1);
        if(insert > 0){
            // 插入成功 后查询对话内容
            List<MessageVO> message = messageService.getMessageByUsername(new MessageDTO(chatConversationDTO.getFriendId(), LocalDateTime.now(), 20));
            chatConversationVO.setMessages(message);
        }
        return chatConversationVO;
    }

    @Override
    public Boolean delete(ChatConversationDTO chatConversationDTO) {
        return chatConversationMapper.deleteById(chatConversationDTO.getUserId()) > 0;
    }

    @Override
    public Boolean show(ChatConversationDTO chatConversationDTO) {
        return chatConversationMapper.updateById(new ChatConversation(chatConversationDTO.getUserId(),0)) > 0;
    }

    @Override
    public List<ChatConversationVO> getAll() {
        List<ChatConversation> chatConversations = chatConversationMapper.selectList(new LambdaQueryWrapper<ChatConversation>().eq(ChatConversation::getUserId, (String) StpUtil.getLoginId()));
        ArrayList<ChatConversationVO> chatConversationVOS = new ArrayList<>();
        for (ChatConversation chatConversation : chatConversations) {
            ChatConversationVO chatConversationVO = new ChatConversationVO();
            BeanUtils.copyProperties(chatConversation,chatConversationVO);
            List<MessageVO> message = messageService.getMessageByUsername(new MessageDTO(chatConversationVO.getFriendId(), LocalDateTime.now(), 20));
            chatConversationVO.setMessages(message);
            chatConversationVOS.add(chatConversationVO);
        }
        return chatConversationVOS;
    }
}
